﻿// Validate US datetime 
function isDateUS(value) {
    var check = false;
    if (value.match(/^\d{1,2}\/\d{1,2}\/\d{4}$/)) {
        var adata = value.split('/');
        var mm = parseInt(adata[0], 10);
        var dd = parseInt(adata[1], 10);
        var yyyy = parseInt(adata[2], 10);
        var xdata = new Date(yyyy, mm - 1, dd);
        if ((xdata.getFullYear() == yyyy) && (xdata.getMonth() == mm - 1) && (xdata.getDate() == dd))
            check = true;
        else
            check = false;
    } else {
        check = false;
    }
    return check;
}

// Validate datetime 
function isDateTime(a) {
    var a = new Date(a);
    return ($.isNumeric(a.getTime()));
}

// Parse datetime to number
function parseDateTime(a) {
    var a = new Date(a);
    return (a.getTime());
}

// Get current date with format : "mm/dd/yy"
function getCurrentDate() {
    var currentTime = new Date()
    var month = currentTime.getMonth() + 1;
    var day = currentTime.getDate();
    var year = currentTime.getFullYear();
    return (month + "/" + day + "/" + year)
}

// Function handle all cross links
// contain ajax helper class to
// using for ajax action link request
function IsNumeric(sText) {
    var ValidChars = "0123456789";
    var IsNumber = true;
    var Char;
    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
}

//cheeck for Decimal
function IsDecimal(sText) {
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;
    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
}

function ShowMessage(message) {
    $("#message").html(message);
    ShowDialog(true);
}

function isNumberKey(evt) {
    evt = evt || window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}

function isPhoneExtNumber(evt) {
    evt = evt || window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    // check charcode, it's ok if user enter space,(,),-, del character (charCode for + is 43)
    if (charCode == 32 || charCode == 45 || charCode == 46)
        return true;

    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}

function isPhoneNumber(evt) {
    evt = evt || window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;

    // check charcode, it's ok if user enter space,(,),-, del character (charCode for + is 43)
    if (charCode == 32 || charCode == 40 || charCode == 41 || charCode == 45 || charCode == 46)
        return true;

    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}

function isCostKey(evt) {
    evt = evt || window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;

    // "Home" , "End"
    if (charCode == 35 || charCode == 36)
        return true;

    // "Del,.=46", "left arrow=37" , "right arrow=39"
    if (charCode == 46 || charCode == 37 || charCode == 39)
        return true;
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}

function isAreaKey(evt) {

    evt = evt || window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;

    // "Home" , "End"
    if (charCode == 35 || charCode == 36)
        return true;

    // "Del,.=46", "left arrow=37" , "right arrow=39"
    if (charCode == 46 || charCode == 37 || charCode == 39)
        return true;
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}


////////////////////////////////////////////////////////////////////////////////
// param: Mask & Value
// Example : formatCurrency("#,##0.00", 343434312321567.886);
////////////////////////////////////////////////////////////////////////////////
function formatCurrency(m, v) {
    if (!m || isNaN(+v)) {
        return v; //return as it is.
    }
    //convert any string to number according to formation sign.
    var v = m.charAt(0) == '-' ? -v : +v;
    var isNegative = v < 0 ? v = -v : 0; //process only abs(), and turn on flag.

    //search for separator for grp & decimal, anything not digit, not +/- sign, not #.
    var result = m.match(/[^\d\-\+#]/g);
    var Decimal = (result && result[result.length - 1]) || '.'; //treat the right most symbol as decimal 
    var Group = (result && result[1] && result[0]) || ',';  //treat the left most symbol as group separator

    //split the decimal for the format string if any.
    var m = m.split(Decimal);
    //Fix the decimal first, toFixed will auto fill trailing zero.
    v = v.toFixed(m[1] && m[1].length);
    v = +(v) + ''; //convert number to string to trim off *all* trailing decimal zero(es)

    //fill back any trailing zero according to format
    var pos_trail_zero = m[1] && m[1].lastIndexOf('0'); //look for last zero in format
    var part = v.split('.');
    //integer will get !part[1]
    if (!part[1] || part[1] && part[1].length <= pos_trail_zero) {
        v = (+v).toFixed(pos_trail_zero + 1);
    }
    var szSep = m[0].split(Group); //look for separator
    m[0] = szSep.join(''); //join back without separator for counting the pos of any leading 0.

    var pos_lead_zero = m[0] && m[0].indexOf('0');
    if (pos_lead_zero > -1) {
        while (part[0].length < (m[0].length - pos_lead_zero)) {
            part[0] = '0' + part[0];
        }
    }
    else if (+part[0] == 0) {
        part[0] = '';
    }

    v = v.split('.');
    v[0] = part[0];

    //process the first group separator from decimal (.) only, the rest ignore.
    //get the length of the last slice of split result.
    var pos_separator = (szSep[1] && szSep[szSep.length - 1].length);
    if (pos_separator) {
        var integer = v[0];
        var str = '';
        var offset = integer.length % pos_separator;
        for (var i = 0, l = integer.length; i < l; i++) {

            str += integer.charAt(i); //ie6 only support charAt for sz.
            //-pos_separator so that won't trail separator on full length
            if (!((i - offset + 1) % pos_separator) && i < l - pos_separator) {
                str += Group;
            }
        }
        v[0] = str;
    }

    v[1] = (m[1] && v[1]) ? Decimal + v[1] : "";
    return (isNegative ? '-' : '') + v[0] + v[1]; //put back any negation and combine integer and fraction.
}

function removeWhiteSpaces(inputString) {
    // Remove the whitespaces at the beginning and end
    var s1 = inputString.replace(/^\s+|\s+$/g, "");
    // Remove all extra space between words in a string
    return s1.replace(/\s+/g, " ");
}

// Get URL parameter by name
function getURLParameter(name) {
    return decodeURIComponent((location.search.match(RegExp("[?|&]" + name + '=(.+?)(&|$)')) || [, null])[1]);
}

// Close popup error request
// When have an action call a ajax function
// And this request is failure
function closePopupError() {
    $('.fancybox-skin').hide();
    $('#fancybox-overlay').hide();
    $.fancybox.close();
}
function closePopup() {
    $('.fancybox-skin').hide();
    $('#fancybox-overlay').hide();
    $.fancybox.close();
}
function showPopupError() {
    $('#fancybox-overlay').show();
    $('#popupErrorRequest').showPopup({
        width: 450
    });
}